<template>
  <div class="image-upscaler">
    <div class="upscaler-container">
      <h1>图片无损放大</h1>
      <img v-if="imageUrl" :src="imageUrl" alt="预览图片" class="image-preview">
      <div class="control-panel">
        <span>放大倍数：</span>
        <div class="slider-wrapper">
          <el-select v-model="scale" placeholder="选择放大倍数" class="scale-select">
            <el-option
              v-for="value in [1, 1.5, 2, 2.5, 3, 3.5, 4]"
              :key="value"
              :label="`${value}x`"
              :value="value"
            />
          </el-select>
        </div>
        <el-button type="primary" @click="upscaleImage" :disabled="!imageUrl">
          <el-icon><ZoomIn /></el-icon>
          放大
        </el-button>
      </div>
      <div class="button-group">
        <el-button type="primary" @click="uploadImage">
          <el-icon><Upload /></el-icon>
          上传
        </el-button>
        <el-button type="success" @click="downloadImage" :disabled="!upscaledImageUrl">
          <el-icon><Download /></el-icon>
          下载
        </el-button>
        <el-button type="warning" @click="goToHomepage">
          <el-icon><Back /></el-icon>
          返回
        </el-button>
      </div>
      <input type="file" ref="fileInput" @change="handleFileUpload" accept="image/*" class="hidden-input">
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ZoomIn, Upload, Download, Back } from '@element-plus/icons-vue'

const router = useRouter()
const fileInput = ref(null)
const imageUrl = ref('')
const upscaledImageUrl = ref('')
const scale = ref(2)

const uploadImage = () => {
  fileInput.value.click()
}

const handleFileUpload = (event) => {
  const file = event.target.files[0]
  if (file) {
    const reader = new FileReader()
    reader.onload = (e) => {
      imageUrl.value = e.target.result
      upscaledImageUrl.value = ''
    }
    reader.readAsDataURL(file)
  }
}

const upscaleImage = () => {
  if (!imageUrl.value) {
    ElMessage.warning('请先上传图片')
    return
  }
  ElMessage.info('图片放大中，请稍候...')
  const img = new Image()
  img.onload = () => {
    const canvas = document.createElement('canvas')
    const ctx = canvas.getContext('2d')
    canvas.width = img.width * scale.value
    canvas.height = img.height * scale.value
    ctx.imageSmoothingEnabled = true
    ctx.imageSmoothingQuality = 'high'
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
    upscaledImageUrl.value = canvas.toDataURL('image/png')
    ElMessage.success('图片放大成功！')
  }
  img.src = imageUrl.value
}

const downloadImage = () => {
  if (upscaledImageUrl.value) {
    const link = document.createElement('a')
    link.href = upscaledImageUrl.value
    link.download = 'upscaled_image.png'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}

const goToHomepage = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.image-upscaler {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 10px;

  .upscaler-container {
    max-width: 600px;
    width: 98%;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
    padding: 25px;
    box-sizing: border-box;
    margin: 0 auto;

    h1 {
      text-align: center;
      color: #4a5568;
      font-size: 2.5em;
      margin: 0 0 20px 0;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    }

    .image-preview {
      max-width: 100%;
      max-height: 300px;
      margin: 12px auto;
      display: block;
      border-radius: 8px;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease;

      &:hover {
        transform: scale(1.02);
      }
    }

    .control-panel {
      display: flex;
      align-items: center;
      gap: 12px;
      margin: 12px 0;
      padding: 12px;
      background-color: #f8fafc;
      border-radius: 12px;

      span {
        white-space: nowrap;
        color: #4a5568;
      }

      .slider-wrapper {
        flex-grow: 1;
        
        .scale-select {
          width: 100%;
        }
      }
    }

    .button-group {
      display: flex;
      justify-content: center;
      gap: 12px;
      margin-top: 20px;

      .el-button {
        flex: 1;
        min-width: 0;
        padding: 8px 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;

        .el-icon {
          margin-right: 4px;
          font-size: 16px;
        }

        span {
          font-size: 14px;
          white-space: nowrap;
        }

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
      }
    }

    .hidden-input {
      display: none;
    }
  }
}

@media (max-width: 480px) {
  .image-upscaler {
    padding: 10px;

    .upscaler-container {
      padding: 15px;
      width: 98%;

      h1 {
        font-size: 24px;
        margin-bottom: 15px;
      }

      .control-panel {
        flex-direction: column;
        padding: 12px;

        .slider-wrapper {
          width: 100%;
          margin: 8px 0;
        }
      }

      .button-group {
        flex-direction: row;
        gap: 8px;
        margin-top: 15px;

        .el-button {
          padding: 6px 8px;

          .el-icon {
            margin-right: 3px;
            font-size: 14px;
          }

          span {
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>
